package com.gxljc.bear.crawler.cli;

import com.gxljc.commons.quartz.QuartzExecutor;
import com.gxljc.commons.util.AdvCli;
import com.gxljc.commons.util.CliRunner;
import com.gxljc.bear.crawler.weixin.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.log4j.Logger;


/**
 * 微信爬虫.
 */
public class WeixinCrawlerCli implements QuartzExecutor, CliRunner {
    public static final Logger LOG = Logger.getLogger(WeixinCrawlerCli.class);

    // 每13分触发
    private static final String CRON_EXP1 = "0 */13 * * * ?";

    // type of running [all|page]
    private String type;
    private boolean proxy = false;
    private boolean update = false;
    private String site;

    @Override
    public Options initOptions() {
        Options options = new Options();
        options.addOption(AdvCli.CLI_PARAM_HELP, false, "WeixinCrawlerCli print help message");
        // run periodically
        options.addOption("quartz", false, "run in quartz mode");
        options.addOption("once", false, "run in once mode");
        options.addOption("proxy", false, "run in proxy mode");
        options.addOption("update", false, "update all page");
        options.addOption("type", true, "all|seed|page");
        options.addOption("site", true, "sogou|newrank|pc");
        return options;
    }

    @Override
    public boolean validateOptions(CommandLine cmdLine) {
        return true;
    }

    @Override
    public void start(CommandLine cmdLine) {
        // if there is no type parameter, do not run the program
        if (cmdLine.hasOption("type")) {
            type = cmdLine.getOptionValue("type");
        } else {
            System.out.println("type parameter is compulsory.");
            return;
        }
        if (cmdLine.hasOption("site")) {
            site = cmdLine.getOptionValue("site");
        } else {
            System.out.println("site parameter is compulsory.");
            return;
        }
        if (cmdLine.hasOption("proxy")) {
            proxy = true;
        }
        if (cmdLine.hasOption("update")) {
            update = true;
        }
        execute();
    }

    @Override
    public void execute() {
        LOG.info("Execute WeixinCrawlerCli.");
        try {
            if (site.equals("sogou")) {
                if ("seed".equals(type)) {
                    SogouAccountCrawler accountCrawler = new SogouAccountCrawler(proxy, update);
                    accountCrawler.crawl();
                } else if ("page".equals(type)) {
                    SogouPageCrawler pageCrawler = new SogouPageCrawler(proxy);
                    pageCrawler.crawl();
                }
            } else if (site.equals("newrank")) {
                if ("seed".equals(type)) {
                    NewRankAccountCrawler accountCrawler = new NewRankAccountCrawler(proxy, update);
                    accountCrawler.crawl();
                } else if ("page".equals(type)) {
                    NewRankPageCrawler pageCrawler = new NewRankPageCrawler(proxy);
                    pageCrawler.crawl();
                }
            } else if (site.equals("pc")) {
                if ("seed".equals(type)) {
                    PcSeedCrawler accountCrawler = new PcSeedCrawler(proxy);
                    accountCrawler.crawl();
                } else if ("page".equals(type)) {
                    PcPageCrawler pageCrawler = new PcPageCrawler(proxy);
                    pageCrawler.crawl();
                }
            } else {
                LOG.error("type is not found: " + type);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.info("Weixin Crawler done.");
    }

    public static void main(String[] args) {
        AdvCli.initRunner(args, "WeixinCrawlerCli", new WeixinCrawlerCli());
    }
}
